﻿// Requires Gw.Core.js and Gw.Equipment.js
Gw.Equipment.Configuration = function(data) {
    var properties = ["Primary", "Secondary", "ArmorAttribute", "WeaponAttribute", "Color", "Armor"];

    for (var i in properties) {
        this[properties[i]] = data[properties[i]];
    }

    return this;
}

Gw.Equipment.Configuration.prototype = {
    getWeaponAttribute: function() {
        if (!this.Primary) return false;
        var attribute = this.Primary.Attributes[this.WeaponAttribute];
        if (attribute) return attribute;

        if (!this.Secondary) return false
        return this.Secondary.Attributes[this.WeaponAttribute];
    },

    getRuneId: function(rune) {
        if (rune.Name) return Gw.Equipment.Mods[rune.Name];
    },

    getArmorIds: function(property) {
        var armorIds = new Array();
        var armorIndex = 0;
        var itemId;

        for (var i in this.Armor) {
            armorIds[armorIndex++] = Gw.Equipment.Mods[this.Armor[i][property]];
        }

        return armorIds;
    },

    getInsigniaIds: function() {
        return this.getArmorIds("Insignia");
    },

    getRuneIds: function() {
        return this.getArmorIds("Rune");
    },

    createTemplate: function(weaponsCount) {
        var armorAttribute = this.Primary.Attributes[this.ArmorAttribute] || this.Primary.Attributes[this.Primary.PrimaryAttribute];
        var headId = armorAttribute.HeadId;
        var insigniaIds = this.getInsigniaIds();
        var runeIds = this.getRuneIds();
        var color = this.Color || 9;

        var template = new Gw.Equipment.Template(color);

        template.writeHeader(5 + weaponsCount);
        template.writeHead(headId, insigniaIds[0], runeIds[0]);
        template.writeChest(this.Primary.ChestId, insigniaIds[1], runeIds[1]);
        template.writeHands(this.Primary.HandsId, insigniaIds[2], runeIds[2]);
        template.writeLegs(this.Primary.LegsId, insigniaIds[3], runeIds[3]);
        template.writeFeet(this.Primary.FeetId, insigniaIds[4], runeIds[4]);

        return template;
    },

    getDefensive: function(spearGrip, sprearInscription, shieldInscription) {
        var spearId = Gw.Equipment.Equipments["PvP Spear"];
        var shieldId = Gw.Equipment.Equipments["PvP Tactics Shield"];

        var template = this.createTemplate(2);

        template.writeWand(spearId, Gw.Equipment.Mods[spearGrip], Gw.Equipment.Mods[sprearInscription]);
        template.writeFocus(shieldId, Gw.Equipment.Mods["Shield Handle of Fortitude"], Gw.Equipment.Mods[shieldInscription]);

        return template.getCode();
    },

    getDef30_5: function(shieldInscription) {
        return this.getDefensive("Spear Grip of Fortitude", "Brawn over Brains Inscription", shieldInscription);
    },

    getDef305: function(shieldInscription) {
        return this.getDefensive("Spear Grip of Fortitude", "I Have The Power Inscription", shieldInscription);
    },

    getDef20_5: function(shieldInscription) {
        return this.getDefensive("Spear Grip of Enchanting", "Brawn over Brains Inscription", shieldInscription);
    },

    getDef205: function(shieldInscription) {
        return this.getDefensive("Spear Grip of Enchanting", "I Have The Power Inscription", shieldInscription);
    },

    get2020Set: function(wandInscription, focusInscription) {
        var weaponAttribute = this.getWeaponAttribute();
        var template = this.createTemplate(2);

        template.writeWand(weaponAttribute.WandId, Gw.Equipment.Mods["Wand Wrapping of Memory"], Gw.Equipment.Mods[wandInscription]);
        template.writeFocus(weaponAttribute.FocusId, Gw.Equipment.Mods["Focus Core of Aptitude"], Gw.Equipment.Mods[focusInscription]);

        return template.getCode();
    },

    get4040: function() {
        return this.get2020Set("Aptitude not Attitude Inscription", "Forget Me Not Inscription");
    },

    get15_1: function() {
        return this.get2020Set("Aptitude not Attitude Inscription", "Live for Today Inscription");
    },

    get30_2: function() {
        return this.get2020Set("Seize the Day Inscription", "Live for Today Inscription");
    },

    get4020Staff: function(wrapping) {
        var weaponAttribute = this.getWeaponAttribute();
        var template = this.createTemplate(1);

        template.writeStaff(weaponAttribute.StaffId, Gw.Equipment.Mods["Adept Staff Head"], Gw.Equipment.Mods[wrapping], Gw.Equipment.Mods["Aptitude not Attitude Inscription"]);

        return template.getCode();
    },

    get402020: function() {
        return this.get4020Staff("Staff Wrapping Enchanting");
    },

    get402030: function() {
        return this.get4020Staff("Staff Wrapping of Fortitude");
    }
};
